package com.headwind.logistics.common.config;

import com.headwind.logistics.log.Listener.MySessionListener;
import com.headwind.logistics.logistics.interceptor.LoginInterceptor;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ClassUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * web配置类
 * @author 小恶魔
 * @dateTime 2020-7-2
 *
 */
@Configuration
public class MyWebConfig extends WebMvcConfigurationSupport {

    @Autowired
    LoginInterceptor loginInterceptor;

    /**
     * 设置静态文件的目录
     * @param registry
     *
     * 此方法不要更改
     */
    @SneakyThrows
    public void addResourceHandlers(ResourceHandlerRegistry registry){
        //获取后台绝对路径静态文件目录
        String adminUrl = ResourceUtils.getURL("src/main/resources/static/admin").getPath();
        //获取前台绝对路径静态文件目录
        String logisticsUrl = ResourceUtils.getURL("src/main/resources/static/logistics").getPath();

        adminUrl = adminUrl.substring(1,adminUrl.length());
        logisticsUrl = logisticsUrl.substring(1,logisticsUrl.length());

        registry.addResourceHandler("/static/admin/**").addResourceLocations("file:"+adminUrl);
        registry.addResourceHandler("/static/logistics/**").addResourceLocations("file:"+logisticsUrl);
    }

    /**
     * 注入拦截器
     * @param registry
     */
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        // TODO Auto-generated method stub
        // addPathPatterns("/**") 表示拦截所有的请求，
        // excludePathPatterns("/login", "/register") 表示除了登陆与注册之外，因为登陆注册不需要登陆也可以访问
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/logistics/**")
                .excludePathPatterns("/","/toLogin","/logistics/customerLogin");

    }

}
